package jomi.fatiasdovos.fatias.parser;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by jorgemachado on 27/12/13.
 */
public class TestFinance {


    private static final Logger logger = Logger.getLogger(TestFinance.class);

    public static void main(String[] args) throws IOException, InterruptedException {

        Collection<Source> sources = Source.getSources(SourcesProperties.getInstance());


        while(true)
        {
            for(Source source: sources)
            {
                try{
                    Document doc = Jsoup.connect(source.getUrl()).get();
                    Element pricepanel = doc.select(source.getGroup().getJqueryCotacao()).first();
                    Element up = doc.select(source.getGroup().getUpCotacao()).first();
                    logger.info("source[" + source.getName() + "] cotacao[" + pricepanel.text() + "] up[" + up.text() + "] time[" + System.currentTimeMillis() + "]");
                    Thread.sleep(1000);
                }
                catch(Throwable e)
                {
                    logger.warn("Source is rejecting wating 5 seconds");
                    Thread.sleep(10000);
                }
            }
            Thread.sleep(5000);
        }

    }

}
